package leetcode.treeCode.week3.leetcode_513;

import java.util.LinkedList;

class Solution {
    public int findBottomLeftValue(TreeNode root) {
        LinkedList<TreeNode> nodes = new LinkedList<>();
        nodes.add(root);
        while(nodes.size()!=0){
            int size = nodes.size();
            TreeNode poll = nodes.poll();
            TreeNode ans = poll;
            if (poll.left != null) {
                nodes.add(poll.left);
            }
            if (poll.right != null) {
                nodes.add(poll.right);
            }
            for (int i =1 ;i<size;i++){
                TreeNode node = nodes.poll();
                if (node.left != null) {
                    nodes.add(node.left);
                }
                if (node.right != null) {
                    nodes.add(node.right);
                }
            }
            if (nodes.size() == 0) {
                return ans.val;
            }
        }
        return -1;
    }
}